﻿using MediatR;
using W.Warehouse.Api.Application.Command.WarehouseCommand;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.WarehouseCommandHandlder
{
    public class WarehouseDelectRangeCommandHandlder : IRequestHandler<WarehouseDelectRangeCommand, ApiResult<int>>
    {
        private readonly ILogger<WarehouseDelectRangeCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public WarehouseDelectRangeCommandHandlder(IWarehouseInfoRepository warehouseInfoRepository, ILogger<WarehouseDelectRangeCommandHandlder> logger)
        {
            _warehouseInfoRepository = warehouseInfoRepository;
            _logger = logger;
        }
        public async Task<ApiResult<int>> Handle(WarehouseDelectRangeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("批量删除仓库管理成功");
                var res = (await _warehouseInfoRepository.GetAllAsync()).Where(x => request.warehouseId.Contains(x.WarehouseId)).ToList();
                res.ForEach(x =>
                {
                    x.IsDelete = true;
                    x.DeleteName = "张三";
                    x.DeleteTime = DateTime.Now;
                });
                result.Code = await _warehouseInfoRepository.UpdateRangeAsync(res);
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
